﻿
#include <vcl.h>
#include <IniFiles.hpp>
#pragma hdrstop

#include "unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "OverbyteIcsWSocket"
#pragma link "OverbyteIcsWndControl"
#pragma link "iComponent"
#pragma link "iCustomComponent"
#pragma link "iSwitchLed"
#pragma link "iSwitchToggle"
#pragma link "iVCLComponent"
#pragma link "iSwitchPanel"
#pragma link "iLed"
#pragma link "iLedRound"
#pragma link "iAngularLogGauge"
#pragma link "iGaugeComponent"
#pragma link "iPositionComponent"
#pragma link "iScaleComponent"
#pragma link "iSwitchRocker"
#pragma link "iSwitchRocker3Way"
#pragma link "iPanel"
#pragma link "iAngularGauge"
#pragma link "iEdit"
#pragma link "iEditCustom"
#pragma link "iLabel"
#pragma link "iTimers"
#pragma resource "*.dfm"

TForm1 *Form1;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
	: TForm(Owner)
{
	DisplayMemo->Clear();
    // 输入框
    iEdit1->Enabled = false;
	iEdit1->Color   = clBtnFace;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	DisplayMemo->Clear();
    iEdit1->Clear();
    iEdit2->Clear();
    if (iEdit1->Enabled) {
    	iEdit1->SetFocus();
    }
}
//---------------------------------------------------------------------------

void TForm1::Display(AnsiString Msg)
{
	if (DisplayMemo->Lines->Count > 500) // 超出则清空
	{
		DisplayMemo->Clear();
	}
	DisplayMemo->Lines->Add(Msg);
}
//---------------------------------------------------------------------------
void TForm1::Connect()
{
	HostNameEdit->Enabled = false;
	WSocket1->Proto = "udp";
	WSocket1->Port = "60000";
	WSocket1->Addr = HostNameEdit->Text;
	WSocket1->Connect();

    WSocket2->Proto = "udp";
	WSocket2->Port = "60001";
	WSocket2->Addr = HostNameEdit->Text;
	WSocket2->Connect();
}
//---------------------------------------------------------------------------

void TForm1::Disconnect()
{
	HostNameEdit->Enabled = true;
	WSocket1->Close();
    WSocket2->Close();

}
//---------------------------------------------------------------------------


void __fastcall TForm1::WSocket1DataAvailable(TObject *Sender,
											  WORD Error)
{
	// 网络接收收据
	int Len;
	char Buf[1600];

	memset(Buf, 0, sizeof(Buf));
	Len = ((TCustomLineWSocket *)Sender)->Receive(Buf, sizeof(Buf) - 1);
	if (Len <= 0)
		return;

	//this->Display(Buf);

	// 1. 检测sn返回值
	if (strncmp(Buf, "sn,", 3) == 0)
	{
		for (int i = 0; i < Len; i++)
		{
			if (Buf[i] == ';')
            	Buf[i] = '\0';
		}
		char *pString = Buf +3;
        AnsiString str;
        str.sprintf(pString);
        this->Display("sn = " + str);
		iEdit2->Value = str ;
	}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::WSocket2DataAvailable(TObject *Sender, WORD ErrCode)
{
	// 网络接收收据
	int Len;
	char Buf[1600];

	memset(Buf, 0, sizeof(Buf));
	Len = ((TCustomLineWSocket *)Sender)->Receive(Buf, sizeof(Buf) - 1);
	if (Len <= 0)
		return;

	//this->Display(Buf);

	// 1. 检测温度 tmp,123000;
	if (strncmp(Buf, "tmp,", 4) == 0)
	{
		for (int i = 0; i < Len; i++)
		{
			if (Buf[i] == ';')
            	Buf[i] = '\0';
		}

		char *pString = Buf + 4;
		int value = strtol(pString, NULL, 0);

		// 超时timer, 关闭
		iTimers1->Enabled2 = false;

		// 指示灯
		if(iLedRound1->Active == false)
		   iLedRound1->Active =  true;		  // 指示灯

        // SN输入框
		if (iEdit1->Enabled == false)
        {
        	iEdit2->Clear();
        	iEdit1->Clear();
    		iEdit1->Enabled = true;
            iEdit1->Color = clWhite;
            iEdit1->SetFocus();
        }

		// 显示温度
		iAngularGauge1->Position = (double)(value/100)/10.0;
		iAngularGauge1->Label1Text = FloatToStr((float)iAngularGauge1->Position) + " ℃";
		if (iAngularGauge1->Visible == false)
			iAngularGauge1->Visible = true;
	}
}
//---------------------------------------------------------------------------

void TForm1::Send(int No, char *buf, int size)
{
	if (No == 0)
		WSocket1->Send((void *)buf, size); 
	else 
		WSocket2->Send((void *)buf, size);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::iTimers1Timer1(TObject *Sender)
{
	// 心跳定时器
	//this->Display("450mS");

	this->Send(1,"tmp,get;", 8);
	//this->Display("tmp,get;");

	if (iTimers1->Enabled2 == false)
		iTimers1->Enabled2 = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::iTimers1Timer2(TObject *Sender)
{
	// 超时定时器
	this->Display("timer out");
	iTimers1->Enabled2 = false;

	if (iLedRound1->Active == true)
		iLedRound1->Active =  false; // 指示灯

	if (iAngularGauge1->Visible == true)
		iAngularGauge1->Visible = false; // 温度

    iEdit2->Clear();     // SN 读出
    iEdit1->Clear();    // SN 写入
    if (iEdit1->Enabled == true)
    {
    	// 输入框
    	iEdit1->Enabled = false;
		iEdit1->Color   = clWhite;

     }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::iSwitchRocker1Change(TObject *Sender)
{
	// 电源开关控制
	if (iSwitchRocker1->Active)  // 开
	{
		// 网络
		this->Connect();
		iTimers1->Enabled1 = true;

        // IP输入框
        HostNameEdit->Enabled = false;
        HostNameEdit->Color   = clBtnFace;
	}
	else 
	{
		// 网络
		this->Disconnect();
		iLedRound1->Active = false;       	// 指示灯
		iAngularGauge1->Visible = false;  	// 温度
		iTimers1->Enabled1 = false;  		// 定时器1
		iTimers1->Enabled2 = false; 		// 定时器2

        // IP输入框
        HostNameEdit->Enabled = true;
        HostNameEdit->Color   = clWhite;

	}
    // 输入框
    iEdit1->Enabled = false;
    iEdit1->Color   = clBtnFace;
    iEdit1->Clear();
	iEdit2->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::iSwitchRocker3Way1Click(TObject *Sender)
{
    this->SendSN(); 
}
//---------------------------------------------------------------------------

void __fastcall TForm1::iSwitchRocker3Way2Click(TObject *Sender)
{
	this->Send(0,"sn,get;", 7);
}
//---------------------------------------------------------------------------

void TForm1::SendSN(void)
{
	if (!(iEdit1->Value.IsEmpty()))
	{
    	AnsiString string =  "sn,set," + iEdit1->Value  + ";" ;
        char *pChar = string.c_str();
        int   size  = string.Length();
		this->Send(0, pChar, size);
		//this->Display(string);
	}
	else 
	{
		this->Display("is empty");
	}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::iEdit1KeyPress(TObject *Sender, wchar_t &Key)
{
	// 有按键，则进入
	char key_value = Key;
	if (key_value == 0x0d)
	{
		this->Display("检测到\\r");
        this->SendSN();
	}
	else if (key_value == 0x0a) {
		/* code */
		this->Display("检测到\\n");
	}
	//else
    //	this->Display(AnsiString(Key));

}
//---------------------------------------------------------------------------



